/**
 * $RCSfile: DbActivityDefFormManager.java,v $
 * $Revision: 1.1 $
 * $Date: 2014/07/08 00:26:04 $
 * <p>
 * Copyright (C) 2003 itjds, Inc. All rights reserved.
 * <p>
 * This software is the proprietary information of itjds, Inc.
 * Use is subject to license terms.
 */
package com.ds.bpm.engine.database.form;

import com.ds.bpm.engine.BPMConstants;
import com.ds.bpm.engine.BPMException;
import com.ds.bpm.engine.inter.EIActivityDef;
import com.ds.bpm.engine.inter.EIActivityDefManager;
import com.ds.bpm.engine.inter.EIAttributeDef;
import com.ds.bpm.engine.inter.EIAttributeDefManager;
import com.ds.common.logging.Log;
import com.ds.common.logging.LogFactory;
import com.ds.enums.CommonYesNoEnum;
import com.ds.enums.attribute.AttributeInterpretClass;
import com.ds.enums.attribute.AttributeName;
import com.ds.enums.attribute.Attributetype;
import com.ds.esb.config.formula.FormulaType;

import java.io.Serializable;
import java.util.UUID;

public class DbActivityDefFormManager implements Serializable {

    private static final Log log = LogFactory.getLog(BPMConstants.CONFIG_KEY, DbActivityDefFormManager.class);

    private static DbActivityDefFormManager instance = new DbActivityDefFormManager();

    public static DbActivityDefFormManager getInstance() {
        return instance;
    }


    public DbActivityDefFormManager() {

    }



    public DbActivityDefForm createActivityDefForm() {
        return new DbActivityDefForm();
    }

    public DbActivityDefForm loadByKey(String activityDefId) throws BPMException {
        DbActivityDefForm rightDef = null;
        EIActivityDefManager manager = EIActivityDefManager.getInstance();
        EIActivityDef activityDef = manager.loadByKey(activityDefId);
        if (activityDef == null) {
            return null;
        }
        rightDef = createActivityDefForm();
        rightDef.setActivityDefId(activityDefId);
        rightDef.setTableSelectedId(activityDef.getAttributeValue(FormulaType.ActionSelectedID.getBaseType().getType() + "." + FormulaType.TableSelectedID.getType()));
        rightDef.setTableSelectedAtt(activityDef.getAttribute(FormulaType.TableSelectedID.getBaseType().getType() + "." +  FormulaType.TableSelectedID.getType()));
        rightDef.setEscomSelectedId(activityDef.getAttributeValue(FormulaType.ESDCOM.getBaseType().getType() + "." + FormulaType.ESDCOM.getType()));
        rightDef.setEscomSelectedAtt(activityDef.getAttribute(FormulaType.ESDCOM.getBaseType().getType() + "." +  FormulaType.ESDCOM.getType()));
        rightDef.setActionSelectedId(activityDef.getAttributeValue( FormulaType.ActionSelectedID.getBaseType().getType() + "." + FormulaType.ActionSelectedID));
        rightDef.setActionSelectedAtt(activityDef.getAttribute(FormulaType.ActionSelectedID.getBaseType().getType() + "." + FormulaType.ActionSelectedID));
        return rightDef;
    }

    private EIAttributeDef createRightAttribute(AttributeName name, String value) {
        EIAttributeDefManager attriuteDefManager = EIAttributeDefManager.getInstance();

        EIAttributeDef eiAtt = attriuteDefManager.createAttributeDef();
        eiAtt.setId(UUID.randomUUID().toString());
        eiAtt.setInterpretClass(AttributeInterpretClass.STRING.getType());
        eiAtt.setCanInstantiate(CommonYesNoEnum.NO.getType());
        eiAtt.setName(name.getType());
        eiAtt.setValue(value);
        eiAtt.setType(Attributetype.PAGE.getType());

        return eiAtt;
    }


}
